<?php

/**
 * Class ApiModel
 *
 * @author Steffen Brem <steffenbrem@gmail.com>
 */
class ApiModel
{
    protected $resource;
    protected $rootElement = 'results';

    /**
     * @var Api
     */
    private $api;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->api = Api::$shared;
    }

    /**
     * @param null|mixed $pk
     * @return mixed
     */
    public function read($pk = null)
    {
        if (is_array($pk))
        {
            $result = $this->api->request($this->resource, null, '__read', array(), $pk);
        }
        else
        {
            $result = $this->api->request($this->resource, $pk, '__read', array());
        }

        return $result[$this->rootElement];
    }

    /**
     * @param array $data
     * @param bool $return
     * @return array
     */
    public function update(array $data = array(), $return = false)
    {
        return $this->api->request($this->resource, null, '__update', $data, array(
            'return' => $return
        ));
    }

    /**
     * Create
     *
     * @param array $data
     * @param bool $return
     */
    public function create(array $data = array(), $return = false)
    {
        $r = $this->api->request($this->resource, null, '__create', $data, array(
            'return' => $return
        ));

        if (isset($r[$this->rootElement]))
        {
            return $r[$this->rootElement];
        }
    }

    /**
     * Delete
     */
    public function delete()
    {

    }
}